package com.shop.loan.model.view;

import com.shop.loan.handler.UserRemarkHandler;
import com.shop.loan.model.Channel;
import com.shop.loan.model.LoanOrderPayLog;
import com.shop.loan.model.vo.UserRemarkVo;
import com.shop.loan.proxy.UserByOrderEndProxy;
import com.shop.loan.util.LoanOrderUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.sub_erupt.*;
import xyz.erupt.annotation.sub_field.*;
import xyz.erupt.annotation.sub_field.sub_edit.*;
import xyz.erupt.jpa.model.BaseModel;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@Entity
@Table(name = "s_user",
        uniqueConstraints = {
                @UniqueConstraint(columnNames = "mobile")
        })
@Erupt(
        name = "用户",
        filter = @Filter("UserByOrderEnd.isDelete = false"),
        orderBy = "id desc",
        power = @Power(add = false, delete = false, edit = false, viewDetails = false),
        dataProxy = UserByOrderEndProxy.class,
        rowOperation = {
                @RowOperation(
                        title = "用户详情",
                        icon = "fa fa-user",
                        mode = RowOperation.Mode.SINGLE,
                        tpl = @Tpl(
                                path = "/tpl/userDetailByUser.html",
                                width = "50%",
                                engine = Tpl.Engine.Thymeleaf
                        ),
                        type = RowOperation.Type.TPL
                ),
                @RowOperation(
                        title = "客户意愿备注",
                        icon = "fa fa-pencil-square-o",
                        mode = RowOperation.Mode.SINGLE,
                        eruptClass = UserRemarkVo.class,
                        operationHandler = UserRemarkHandler.class
                ),
        },
        drills = {
                @Drill(code = "pay_bill",
                        title = "还款记录",
                        icon = "fa fa-paypal",
                        link = @Link(linkErupt = LoanOrderPayLog.class,  //关联表
                                joinColumn = "userId")),  //关联表达式
        }
)
@SQLDelete(sql = "update s_user set is_delete = true, delete_time = now() where id = ?")
public class UserByOrderEnd extends BaseModel {

    @Transient
    @EruptField(
            edit = @Edit(title = "基本信息", type = EditType.DIVIDE)
    )
    private String divide_base;

    @EruptField(
            views = @View(title = "id", width = "60px"),
            edit = @Edit(title = "id", search = @Search(), readonly = @Readonly)
    )
    @Column(name = "id", insertable = false, updatable = false)
    private String userId;

    @EruptField(
            edit = @Edit(title = "昵称")
    )
    private String name;

    @Transient
    @EruptField(
            views = @View(title = "用户", type = ViewType.SAFE_TEXT, width = LoanOrderUtil.orderListByUserWidth)
    )
    private String userInfo;

    @EruptField(
            edit = @Edit(title = "姓名", search = @Search(vague = true))
    )
    private String cardName;

    @EruptField(
            edit = @Edit(title = "身份证号码", search = @Search(vague = true))
    )
    private String cardNum;

    @EruptField(
            edit = @Edit(title = "手机号码", search = @Search(vague = true))
    )
    private String mobile;


    @EruptField(
            views = @View(title = "年龄", width = "60px"),
            edit = @Edit(title = "年龄", search = @Search(vague = true))
    )
    private Integer age;

    @EruptField(
            views = @View(title = "性别", width = "60px"),
            edit = @Edit(
                    title = "性别",
                    type = EditType.CHOICE,
                    search = @Search,
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "男", value = "1"),
                                    @VL(label = "女", value = "2"),
                                    @VL(label = "未知", value = "99"),
                            }
                    )
            )
    )
    private Integer gender;

    @ManyToOne
    @EruptField(
            views = @View(title = "注册渠道", column = "name", width = "100px"),
            edit = @Edit(
                    title = "注册渠道",
                    search = @Search(vague = true),
                    type = EditType.REFERENCE_TREE
            )
    )
    @JoinColumn(name = "channel_id")
    private Channel channel;

    @EruptField(
            views = @View(title = "客户意愿", width = LoanOrderUtil.orderListBySignStatusWidth),
            edit = @Edit(
                    title = "客户意愿",
                    type = EditType.CHOICE,
                    search = @Search(),
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "有复借意向", value = "1"),
                                    @VL(label = "无复借意向", value = "2"),
                                    @VL(label = "无人接听", value = "3"),
                                    @VL(label = "其他", value = "9"),
                            }
                    ))
    )
    private Integer userWill;

    @EruptField(
            views = @View(title = "备注", type = ViewType.SAFE_TEXT, width = LoanOrderUtil.orderListByUserWidth),
            edit = @Edit(title = "备注", type = EditType.TEXTAREA)
    )
    private String remark;

    @EruptField(
            views = @View(title = "风控状态"),
            edit = @Edit(
                    title = "风控状态",
                    type = EditType.BOOLEAN,
                    search = @Search(),
                    boolType = @BoolType(
                            trueText = "通过",
                            falseText = "命中"
                    )
            )
    )
    private Boolean machineStatus = true;

    @EruptField(
            views = @View(title = "状态", width = "80px"),
            edit = @Edit(
                    title = "状态",
                    type = EditType.CHOICE,
                    search = @Search,
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "拉黑", value = "0"),
                                    @VL(label = "正常", value = "1"),
                                    @VL(label = "注销", value = "2"),
                            }
                    )
            )
    )
    private Integer userStatus;


    @EruptField(
            views = @View(title = "进行中订单上限", width = "150px"),
            edit = @Edit(title = "进行中订单上限")
    )
    private Integer ingNum;


    private String email;

    @EruptField(
            views = @View(title = "订单结清数量", width = "100px"),
            edit = @Edit(title = "订单结清数量")
    )
    private Integer orderEndNum;

    @Transient
    @EruptField(
            views = @View(title = "结清时间"),
            edit = @Edit(title = "结清时间",
                    readonly = @Readonly,
                    dateType = @DateType(type = DateType.Type.DATE_TIME))
    )
    private Date finishTime;

    @EruptField(
            views = @View(title = "注册时间"),
            edit = @Edit(title = "注册时间",
                    readonly = @Readonly,
                    dateType = @DateType(type = DateType.Type.DATE_TIME))
    )
    private Date createTime;

    private Boolean isDelete = false;

    private Date deleteTime;


    public UserByOrderEnd(Long id) {
        setId(id);
    }

    public UserByOrderEnd() {
    }
}
